#pragma once
#include<iostream>
using namespace std;


namespace key
{
	template<class K>
	struct BSTNode
	{
		BSTNode(const K& key)
			:_key(key)
			, _left(nullptr)
			, _right(nullptr)
		{

		}

		K _key;
		BSTNode<K>* _left;
		BSTNode<K>* _right;
	};

	template<class K>
	class BSTree
	{
		using Node = BSTNode<K>;
	public:
		BSTree() = default;

		~BSTree()
		{
			Destroy(_root);
			_root = nullptr;
		}

		BSTree(const BSTree<K>& t)
		{
			_root = Copy(t._root);
		}

		BSTree<K>& operator=(BSTree<K> t)
		{
			swap(_root,t._root);
			return *this;
		}

		bool Insert(const K& key)
		{
			if (nullptr == _root)
			{
				_root = new Node(key);
				return true;
			}
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (key > cur->_key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}
			cur = new Node(key);
			if (key > parent->_key)
			{
				parent->_right = cur;
			}
			else
			{
				parent->_left = cur;
			}

			return true;
		}

		bool Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (key > cur->_key)
				{
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					cur = cur->_left;
				}
				else
				{
					return true;
				}
			}

			return false;
		}

		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (key > cur->_key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (nullptr == parent)
						{
							_root = cur->_right;
						}
						else
						{
							if (key > parent->_key)
							{
								parent->_right = cur->_right;
							}
							else
							{
								parent->_left = cur->_right;
							}
						}
						delete cur;

						return true;
					}
					else if (nullptr == cur->_right)
					{
						if (nullptr == parent)
						{
							_root = cur->_left;
						}
						else
						{
							if (key > parent->_key)
							{
								parent->_right = cur->_left;
							}
							else
							{
								parent->_left = cur->_left;
							}

						}
						delete cur;

						return true;
					}
					else
					{
						Node* replace = cur->_right;
						Node* replaceparent = cur;
						while (replace->_left)
						{
							replaceparent = replace;
							replace = replace->_left;
						}
						cur->_key = replace->_key;
						if (replaceparent == cur)
						{
							replaceparent->_right = replace->_right;
						}
						else
						{
							replaceparent->_left = replace->_right;
						}
						delete replace;

						return true;
					}
				}
			}

			return false;
		}



		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

	private:
		void _InOrder(Node* root)
		{
			if (nullptr == root)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_key << ' ';
			_InOrder(root->_right);
		}

		void Destroy(Node* root)
		{
			if (nullptr == root)
			{
				return;
			}
			Destroy(root->_left);
			Destroy(root->_right);
			delete root;
		}

		Node* Copy(Node* root)
		{
			if (nullptr == root)
			{
				return nullptr;
			}
			Node* newroot = new Node(root->_key);
			newroot->_left = Copy(root->_left);
			newroot->_right = Copy(root->_right);
			return newroot;
		}

		Node* _root = nullptr;
	};
}

namespace key_value
{
	template<class K,class V>
	struct BSTNode
	{
		BSTNode(const K& key,const V& value)
			:_key(key)
			,_value(value)
			, _left(nullptr)
			, _right(nullptr)
		{

		}

		K _key;
		V _value;
		BSTNode<K,V>* _left;
		BSTNode<K,V>* _right;
	};

	template<class K,class V>
	class BSTree
	{
		using Node = BSTNode<K,V>;
	public:
		BSTree() = default;

		~BSTree()
		{
			Destroy(_root);
			_root = nullptr;
		}

		BSTree(const BSTree<K,V>& t)
		{
			_root = Copy(t._root);
		}

		BSTree<K,V>& operator=(BSTree<K,V> t)
		{
			swap(_root, t._root);
			return *this;
		}

		bool Insert(const K& key,const V& value)
		{
			if (nullptr == _root)
			{
				_root = new Node(key,value);
				return true;
			}
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (key > cur->_key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}
			cur = new Node(key,value);
			if (key > parent->_key)
			{
				parent->_right = cur;
			}
			else
			{
				parent->_left = cur;
			}

			return true;
		}

		Node* Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (key > cur->_key)
				{
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					cur = cur->_left;
				}
				else
				{
					return cur;
				}
			}

			return nullptr;
		}

		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (key > cur->_key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (nullptr == parent)
						{
							_root = cur->_right;
						}
						else
						{
							if (key > parent->_key)
							{
								parent->_right = cur->_right;
							}
							else
							{
								parent->_left = cur->_right;
							}
						}
						delete cur;

						return true;
					}
					else if (nullptr == cur->_right)
					{
						if (nullptr == parent)
						{
							_root = cur->_left;
						}
						else
						{
							if (key > parent->_key)
							{
								parent->_right = cur->_left;
							}
							else
							{
								parent->_left = cur->_left;
							}

						}
						delete cur;

						return true;
					}
					else
					{
						Node* replace = cur->_right;
						Node* replaceparent = cur;
						while (replace->_left)
						{
							replaceparent = replace;
							replace = replace->_left;
						}
						cur->_key = replace->_key;
						if (replaceparent == cur)
						{
							replaceparent->_right = replace->_right;
						}
						else
						{
							replaceparent->_left = replace->_right;
						}
						delete replace;

						return true;
					}
				}
			}

			return false;
		}



		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

	private:
		void _InOrder(Node* root)
		{
			if (nullptr == root)
			{
				return;
			}
			_InOrder(root->_left);
			cout << root->_key << ':'<<root->_value<<' ';
			_InOrder(root->_right);
		}

		void Destroy(Node* root)
		{
			if (nullptr == root)
			{
				return;
			}
			Destroy(root->_left);
			Destroy(root->_right);
			delete root;
		}

		Node* Copy(Node* root)
		{
			if (nullptr == root)
			{
				return nullptr;
			}
			Node* newroot = new Node(root->_key,root->_value);
			newroot->_left = Copy(root->_left);
			newroot->_right = Copy(root->_right);
			return newroot;
		}

		Node* _root = nullptr;
	};
}
